VB Sample Code for Previewing a Report

Private Sub mnuPrintCustomPreview_Click()
Dim result%, jobnum%, mainjob%, printcontrols%

result% = PEOpenEngine()
If result% = 0 Then
MsgBox "Could not start the report engine. Execution must halt.",
vbOKOnly + vbCritical, "Serious Error"
End
End If

jobnum% = PEOpenPrintJob(lblReportName.Caption) ' Filename from label on
Sample form
ErrorTrap "OpenPrintJob in PrintPreview", jobnum%

' Subreport check - if a subreport is currently selected on the main form,
jobnum% becomes the subreport
If lblSubreportName.Visible Then
mainjob% = jobnum%
jobnum% = PEOpenSubreport(mainjob%, lblSubreportName.Caption)
ErrorTrap "OpenSubReport in PrintPreview", mainjob%
End If

' While you can specify exact values for the left, top, width and height of
the Preview form, in most cases you'll want to use the default values,
which scale appropriately for the system being used and the report
' These global constants are in the SAMPLE.BAS file
result% = PEOutputToWindow(jobnum%, "Crystal Reports Preview" & Chr$(0), CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, WS_VISIBLE + WS_CAPTION + WS_BORDER + WS_SYSMENU + WS_THICKFRAME + WS_MINIMIZEBOX + WS_MAXIMIZEBOX, Sample.hWnd)
ErrorTrap "OutputtoWindow in PrintPreview", jobnum%

' Before the print job begins, disable the menu items that would cause
trouble
mnuFileOpen.Enabled = False
mnuFileClose.Enabled = False
mnuFileSubReport.Enabled = False
mnuPrint.Enabled = False
mnuSorting.Enabled = False
mnuEdit.Enabled = False
mnuFormat.Enabled = False
mnuTableSQL.Enabled = False

result% = PEStartPrintJob(jobnum%, True) ' True indicates wait until job is
finished before proceeding
ErrorTrap "StartPrintJob in PrintPreview", jobnum%

' Once the job has started, you must delay closing the job until the user has
' finished viewing the preview window. By monitoring the existence of a
handle
' for the preview window, execution will proceed when the window is
destroyed.
' ie - when the user has finished examining the report and pressed close or
print
result% = 1
Do While result% <> 0
' If a pro edition, we can create our own preview controls, and not use
the Crystal ones
' If the print controls are showing, get rid of them, and show the
' PreviewButtons form
result% = PEPrintControlsShowing(jobnum%, printcontrols%)
ErrorTrap "PrintControlsShowing in PrintPreview", jobnum%
If printcontrols% = 1 Then
printcontrols% = 0
result% = PEShowPrintControls(jobnum%, printcontrols%)
ErrorTrap "ShowPrintControls in PrintPreview", jobnum%
Load PreviewButtons
CenterForm Sample, PreviewButtons
PreviewButtons.Show
End If
' Check for any button presses - pressing a button loads the form tag
' with the name of the button
if Len(PreviewButtons.Tag) <> 0 Then
Select Case PreviewButtons.Tag
Case "Last Page"
result% = PEShowLastPage(jobnum%)
Case "Prev Page"
result% = PEShowPreviousPage(jobnum%)
Case "Next Page"
result% = PEShowNextPage(jobnum%)
Case "First Page"
result% = PEShowFirstPage(jobnum%)
Case "Goto Page"
' Before going to a page, check to see if the page number
' is valid
' Find the maximum page number first
printcontrols% = PEGetNPages(jobnum%)
ErrorTrap "GetNPages in PrintPreview", jobnum%
' If the page number entered is less than 1, set it to 1
If Val(PreviewButtons!txtPage.Text) < 1 Then
PreviewButtons!txtPage.Text = "1"
' Otherwise, if its greater than the maximum page number,
set it to that
ElseIf Val(PreviewButtons!txtPage.Text) > printcontrols% Then
PreviewButtons!txtPage.Text = printcontrols%
End If
' Get the page number the user wishes to go to and set it
printcontrols% = Val(PreviewButtons!txtPage.Text)
result% = PEShowNthPage(jobnum%, printcontrols%)
Case "Next Zoom"
result% = PENextPrintWindowMagnification(jobnum%)
Case "Set Zoom"
' Set Zoom can be set using 25-400% or default values
' from the radio buttons on the form
' First check if there is a value in the Zoom percentage,
and if there is, make it valid
' By confining its range to 25-400
If Len(Trim(PreviewButtons!txtZoom.Text)) <> 0 Then
If Val(PreviewButtons!txtZoom.Text) < 25 Then
PreviewButtons!txtZoom.Text = "25"
ElseIf Val(PreviewButtons!txtZoom.Text) > 400 Then
PreviewButtons!txtZoom.Text = "400"
End If
printcontrols% = Val(PreviewButtons!txtZoom.Text)
' If there isn't a value in the Zoom percentage text box,
' use the radio buttons
ElseIf PreviewButtons!optZoom(0).Value Then
printcontrols% = PE_ZOOM_FULL_SIZE
ElseIf PreviewButtons!optZoom(1).Value Then
printcontrols% = PE_ZOOM_SIZE_FIT_ONE_SIDE
ElseIf PreviewButtons!optZoom(2).Value Then
printcontrols% = PE_ZOOM_SIZE_FIT_BOTH_SIDES
End If
' printcontrols has the appropriate value for the zoom,
' now set the zoom
result% = PEZoomPreviewWindow(jobnum%, printcontrols%)
Case "Print"
result% = PEPrintWindow(jobnum%, True)
Case "Export"
result% = PEExportPrintWindow(jobnum%, False, True)
Case "Close"
PECloseWindow jobnum%
End Select
' This error trap covers the last call (in most cases, the only
' call) in the select case list
ErrorTrap PreviewButtons.Tag & " Button press in PrintPreview", _
jobnum%
' Clear the tag for the next button press
PreviewButtons.Tag = ""
End If
DoEvents
result% = PEGetWindowHandle(jobnum%)
Loop

' Whether its loaded or not, make sure the PreviewButtons form is unloaded
Unload PreviewButtons

' Restore the menu items
mnuFileOpen.Enabled = True
mnuFileClose.Enabled = True
mnuFileSubReport.Enabled = True
mnuPrint.Enabled = True
mnuSorting.Enabled = True
mnuEdit.Enabled = True
mnuFormat.Enabled = True
mnuTableSQL.Enabled = True

' Subreport check - if a subreport is currently open, call PreviewReport to
offer a chance to preview the main report, then close the subreport and
main report
If lblSubreportName.Visible Then
result% = PECloseSubreport(jobnum%)
ErrorTrap "CloseSubReport in PrintPreview", mainjob%
PEClosePrintJob mainjob%
Else
PEClosePrintJob jobnum%
End If

PECloseEngine

MsgBox "Preview Complete!", vbOKOnly, "Operation Succeeded"

End Sub

ActiveX
Private Sub mnuPrintPreview_Click()
Dim hwndPreviewWindow As Long

CrystalReport1.ReportFileName = lblReportName.Caption ' Name from label
on sample form

CrystalReport1.Destination = 0 ' Window

' Set Output Window parameters
CrystalReport1.WindowBorderStyle = 2 ' Sizable
CrystalReport1.WindowControlBox = True
CrystalReport1.WindowControls = True
CrystalReport1.WindowMaxButton = True
CrystalReport1.WindowMinButton = True
CrystalReport1.WindowState = 0 ' Normal
CrystalReport1.WindowTitle = "Crystal Reports 5.0 Preview"
CrystalReport1.WindowHeight = 500
CrystalReport1.WindowWidth = 500
CrystalReport1.WindowTop = 100
CrystalReport1.WindowLeft = 100

CrystalReport1.Action = 1 ' Print

' Get handle for preview window (should be the active window, it will have
been just created)
hwndPreviewWindow = GetActiveWindow()

' Keep checking that the handle is still valid - as long as it is, the Window
still exists
' and execution is stopped
PreviewButtons.Show
Do While IsWindow(hwndPreviewWindow)
DoEvents
If Len(PreviewButtons.Tag) <> 0 Then
Select Case PreviewButtons.Tag
Case "Last Page"
CrystalReport1.PageLast
Case "Prev Page"
CrystalReport1.PagePrevious
Case "Next Page"
CrystalReport1.PageNext
Case "First Page"
CrystalReport1.PageFirst
Case "Goto Page"
CrystalReport1.PageShow (Val(PreviewButtons!txtPage.Text))
Case "Zoom"
CrystalReport1.PageZoom (Val(PreviewButtons!txtZoom.Text))
End Select
PreviewButtons.Tag = ""
End If
Loop
Unload PreviewButtons

' Close the report
CrystalReport1.ReportFileName = ""

MsgBox "Preview Complete!", vbOKOnly, "Operation Completed"

End Sub


Seagate Software IMG Holdings, Inc.
http://www.seagatesoftware.com
Support services:
http://support.seagatesoftware.com